package lew.bing.gwalo;

import com.google.common.util.concurrent.RateLimiter;
import lew.bing.gwalo.rateLimiter.DefaultLimiter;
import lew.bing.gwalo.rateLimiter.Limiter;
import lew.bing.gwalo.semaphoreLimiter.DefaultPermits;
import lew.bing.gwalo.semaphoreLimiter.Permits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 刘国兵 on 2018/7/8.
 * 配置bingo的
 */
@Configuration
public class BingoConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(BingoConfiguration.class);

    public static Map<String,Integer> permitsMap = new HashMap<>();

    public static Map<String,Integer> rateMap = new HashMap<>();

    @ConditionalOnMissingBean(Permits.class)
    @Bean
    public Permits permits() {
        logger.info("创建默认的permits");
        return new DefaultPermits() {
            @Override
            public Map<String, Integer> permitsMap() {
                return permitsMap;
            }
        };
    }

    @ConditionalOnMissingBean(Limiter.class)
    @ConditionalOnClass(RateLimiter.class)
    @Bean
    public Limiter limiter() {
        return new DefaultLimiter() {
            @Override
            public Map<String, Integer> rates() {
                return rateMap;
            }
        };
    }

}
